package com.vpen.springsecuritytest.handler;


import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * -@Version 1.0
 * -@Author:Vpen
 * -@Date:2021/3/15 10:44
 * -@Content:
 * 自定义访问受限403异常
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        // 设置响应状态吗
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.setContentType("text/html;charset=UTF-8");
        // 响应输出
        response.getWriter().print("<html><body><h1>权限不足</h1><body></html>");
        response.getWriter().flush();
    }
}
